package com.core.reach.cloudhub.controller;

import com.core.reach.cloudhub.service.PaymentHystrixService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author ga.zhang
 * @version V1.0
 * @description: TODO
 * @date 2020/3/26 9:46
 **/
@Slf4j
@RestController
@DefaultProperties(defaultFallback = "globalPaymentExceptionHandler")
public class OrderHystrixController {

    @Resource
    private PaymentHystrixService paymentHystrixService;

    @GetMapping("/consumer/hystrix/ok/{id}")
    public String paymentInfoOk(@PathVariable("id") Long id){
        return paymentHystrixService.paymentInfoOk(id);
    }

//    @HystrixCommand(fallbackMethod = "paymentInfoTimeOutHandler",commandProperties = {
//            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "6000")
//    })

    @HystrixCommand
    @GetMapping("/consumer/hystrix/timeout/{id}")
    public String paymentInfoTimeOut(@PathVariable("id") Long id){
        int i = 10 / 0;
        return paymentHystrixService.paymentInfoTimeOut(id);
    }

    public String paymentInfoTimeOutHandler(@PathVariable("id") Long id){
        return "我是消费者80,支付系统繁忙或系统出错，请10秒后重试，(╥╯^╰╥)";
    }

    public String globalPaymentExceptionHandler(){
        return "Global exception handler info，请10秒后重试，(╥╯^╰╥)";
    }

}
